const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  
  try {
    const { id } = event

    // 获取记录信息
    const record = await db.collection('cooking')
      .doc(id)
      .get()

    if (!record.data || record.data._openid !== wxContext.OPENID) {
      throw new Error('无权限删除')
    }

    // 删除云存储中的图片
    if (record.data.images && record.data.images.length) {
      await cloud.deleteFile({
        fileList: record.data.images
      })
    }

    // 删除记录
    await db.collection('cooking')
      .doc(id)
      .remove()

    return {
      success: true
    }

  } catch (err) {
    console.error('[deleteCookingRecord] 错误:', err)
    return {
      success: false,
      error: err.message
    }
  }
} 